package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.InspectionPageReq;
import appapi.Request.ProjPageReq;
import appapi.Request.ProjReq;
import appapi.Request.record.DelOnsiteRecReq;
import appapi.Vo.GetEquipOperPageVo;
import appapi.Vo.OnsitPageVo;
import appapi.Vo.record.InspectWarnVo;
import appapi.Vo.record.OnSiteRecVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.config.MyLog;
import appapi.dto.OnsiteRecModel;
import appapi.entity.Zhongjiao.OnsiteinspectionDto;
import appapi.entity.basic.UserInfo;
import appapi.entity.record.MonthInspectionModel;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.IOnsiteinspectionItemService;
import appapi.service.IOnsiteinspectionService;
import appapi.service.IStaffService;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/06/02 19:39
 */

@Api(tags = "设备巡检记录")
@RestController
@RequestMapping(value = "/api/onSiteInspection")
public class OnSiteInspectionController {

    @Resource
    private IOnsiteinspectionService iOnsiteinspectionService;
    @Resource
    private IOnsiteinspectionItemService iOnsiteinspectionItemService;
    @Resource
    IStaffService iStaffService;

    /**
     * 设备巡检的分页查询
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "巡检--分页查询", notes = "巡检--分页查询")
    @GetMapping(value = "/GetInspectionListByPage")
    public ResponseBean<OnsitPageVo> GetInspectionListByPage(@CurrentUser UserInfo userInfo, InspectionPageReq inspectionPageReq) {
        try {
            if (inspectionPageReq.getCurrPage() == null || inspectionPageReq.getPageSize() == null) {
                inspectionPageReq.setCurrPage(1);
                inspectionPageReq.setPageSize(10);
            }
            //如果什么都没传,则查到该账号的所属机构显示数据
            if (inspectionPageReq.getOrgaPathIds() == null) {
                //拿到人员信息
                Map<String, Object> staffDto = iStaffService.SelectStaffDetail(userInfo.getUserid());
                String orgaPathIds = staffDto.get("orgaPathIds").toString();
                inspectionPageReq.setOrgaPathIds(orgaPathIds);
            }
            List<OnsitPageVo> list = iOnsiteinspectionService.SelectInspectionPage(inspectionPageReq);

            PageInfo pageInfo = new PageInfo(list, inspectionPageReq.getPageSize());

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("设备巡检分页异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 设备巡检导出
     *
     * @return
     * @auther ygp 2023/6/13
     */
    @ApiOperation(value = "设备巡检导出", notes = "设备巡检导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/InspectionRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    @MyLog(title = "设备巡检导出",businessType = BusinessTypeEnum.export)
    public ResponseBean InspectionRecExport(InspectionPageReq req, HttpServletResponse response) {
        try {
            List<OnsitPageVo> list = iOnsiteinspectionService.getList(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "设备巡检", "设备巡检sheet", OnsitPageVo.class, "设备巡检", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("设备巡检导出异常");
        }
    }

    /**
     * 巡检记录列表
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "巡检--记录列表", notes = "巡检--记录列表")
    @GetMapping(value = "/GetInspectionList")
    @ApiImplicitParam(paramType = "query", name = "equipId", dataType = "Long", value = "设备id")
    public ResponseModel GetInspectionList(Long equipId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            List<Map<String, Object>> list = iOnsiteinspectionService.SelectInspectionList(equipId);

            responseModel.setData(list);
            responseModel.setRcount(list.size());
        } catch (BusinessException exception) {

            responseModel.setMessage("巡检记录列表异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 获取巡检详情
     *
     * @return
     * @author lc 2023/06/05
     */
    @ApiOperation(value = "巡检--获取详情", notes = "巡检--获取详情")
    @GetMapping(value = "/GetDetail")
    @ApiImplicitParam(paramType = "query", name = "onSiteId", dataType = "Long", value = "记录id")
    public ResponseBean<OnsiteRecModel> GetDetail(Long onSiteId) {
        try {
            OnsiteRecModel resData = iOnsiteinspectionService.getDetail(onSiteId);
            if (resData == null) {
                return ResponseBean.success("您查询的记录为空");
            }
            return ResponseBean.success(resData);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取记录详情异常: ".concat(exception.getMessage()));
        }
    }


    /**
     * 添加巡检记录
     *
     * @return
     * @auther djq 2023/5/20
     */
    @ApiOperation(value = "巡检--添加记录", notes = "巡检--添加记录")
    @PostMapping(value = "/AddInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "vo", dataType = "object", value = "巡检记录参数")
    })
    @MyLog(title = "添加巡检记录",businessType = BusinessTypeEnum.insert)
    public ResponseBean AddInspection(@CurrentUser UserInfo userInfo, @Validated @RequestBody OnSiteRecVo vo) {
        try {
            vo.setStaffId(userInfo.getUserid());
            iOnsiteinspectionService.InsertInspection(vo);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("添加巡检记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 编辑巡检记录
     *
     * @return
     * @author djq 2023/5/17
     */
    @ApiOperation(value = "巡检--编辑记录", notes = "巡检--编辑记录")
    @PostMapping(value = "/EditInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "vo", dataType = "object", value = "巡检记录参数")
    })
    @MyLog(title = "巡检编辑记录",businessType = BusinessTypeEnum.update)
    public ResponseBean EditInspection(@RequestBody OnSiteRecVo vo) {
        try {
            iOnsiteinspectionService.UpdateInspection(vo);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("编辑巡检记录异常".concat(exception.getMessage()));
        }
    }

    /**
     * 删除巡检记录
     *
     * @return
     * @author djq 2023/5/17
     */
    @ApiOperation(value = "巡检--删除记录", notes = "巡检--删除记录")
    @PostMapping(value = "/DeleteInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "req", dataType = "object", value = "记录id")
    })
    @MyLog(title = "巡检删除记录",businessType = BusinessTypeEnum.delete)
    public ResponseBean DeleteInspection(@RequestBody DelOnsiteRecReq req) {
        try {
            iOnsiteinspectionService.DelInspection(req.getOnsiteIds());
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("删除巡检记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 获取巡检统计
     *
     * @return
     * @author djq 2023/06/06
     */
    @ApiOperation(value = "巡检--获取巡检统计", notes = "巡检--获取巡检统计")
    @GetMapping(value = "/GetWarn")
    public ResponseBean<OnsiteRecModel> GetWarn(@CurrentUser UserInfo userInfo) {
        try {
            InspectWarnVo resData = iOnsiteinspectionService.GetWarn(userInfo);
            return ResponseBean.success(resData);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取获取巡检统计异常: ".concat(exception.getMessage()));
        }
    }
}
